﻿using System.ServiceModel;
using System.Xml;
using System.Diagnostics;

using ScreenMonkey.Net;
using ScreenMonkey.Net.Bindings;
using System;
using System.Collections.Generic;
using ScreenMonkey.Net.Proxies;

namespace ScreenMonkey.Remote
{
    /// <summary>
    /// SMConnection code taken from the Screen Monkey remote
    /// sample with only minor changes.
    /// </summary>

    public class SMConnection:SmNetConnectionManager
    {
        private SmNetClipLibrary clipLibrary = null;
        private SmNetClips clips = null;
        private SmNetScreen screen = null;
        private SmNetPanel panel = null;
        private SmNetLayers layers = null;

        #region Single Instance
                
        private static SMConnection _instance = null;

        public static SMConnection GetInstance()
        {
            if (_instance == null)
            {
                _instance = new SMConnection();
            }

            return _instance;
        }

        #endregion

        public SMConnection()
        {
        }

        public override void Connect()
        {
            NetTcpBinding tcpBinding = base.CreateTcpBinding();

            base.Disconnect();

            base.AddService(clipLibrary = new SmNetClipLibrary(tcpBinding, new EndpointAddress("net.tcp://localhost:4000/ScreenMonkey/ClipLibrary")));
            base.AddService(screen = new SmNetScreen(tcpBinding, new EndpointAddress("net.tcp://localhost:4000/ScreenMonkey/Screen")));
            base.AddService(panel = new SmNetPanel(tcpBinding, new EndpointAddress("net.tcp://localhost:4000/ScreenMonkey/Panel")));
            base.AddService(clips = new SmNetClips(tcpBinding, new EndpointAddress("net.tcp://localhost:4000/ScreenMonkey/Clips")));
            base.AddService(layers = new SmNetLayers(tcpBinding, new EndpointAddress("net.tcp://localhost:4000/ScreenMonkey/Layers")));

            base.Connect();
        }

        public SmNetClips Clips
        {
            get { return clips; }
        }

        public SmNetClipLibrary ClipLibrary
        {
            get { return clipLibrary; }
        }

        public SmNetScreen Screen
        {
            get { return screen; }
        }

        public SmNetPanel Panel
        {
            get { return panel; }
        }

        public SmNetLayers Layers
        {
            get { return layers; }
        }
    }
}
